package com.sheng.controller;

/**
 * @Author: 王胜
 * @Date 2022/12/1 16:19
 */
import com.sheng.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/common")
@Api(tags = "文件上传下载接口")
public class CommonController {

    /**
     * 去YML文件里找
     */
    @Value("${web.uploadPath}")
    private String basePath;

    /**
     * 上传
     *
     * @param file
     * @throws IOException
     */
    @PostMapping("/uploadJmeterWindows")
    @ApiOperation("上传Jmeter文件至windows接口")
    public Result<String> upload(MultipartFile file) throws IOException {
        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        log.info("原始文件名:{}", originalFilename);
        String newJmeterName = "@" + originalFilename;
        log.info("新文件名:{}", newJmeterName);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式

        String date = df.format(new Date());// new Date()为获取当前系统时间

        String JmeterPath = basePath + originalFilename + "(" + date + ")";

        // 判断当前jmeter存储目录是否存在，不存在创建一个
        File directory = new File(JmeterPath);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        // 保存文件到指定位置
        file.transferTo(new File(JmeterPath + "/" + newJmeterName));
        return new Result(200,"上传成功",JmeterPath + "/" + newJmeterName);
    }
}
